﻿/**************************************************************************
创建日期:	2019/7/21 22:07:17     
作	  者:	张存
邮 	  箱:	zhangcunliang@126.com
创建时间:	
Copyright (c)    ZhCun.cn

描	述：
记	录：
***************************************************************************/
using StackExchange.Redis;
using ZhCun.DbCore.Cores;
using ZhCun.DBModel;
using ZhCun.RightsManage.DBModel;
using ZhCun.Utils;
using ZhCun.Utils.Helpers;
using ZhCun.WebUtils.DB;
using ZhCun.WebUtils.VModel;

namespace ZhCun.RightsManage.BLL
{
    public class Menu_ListArg : ArgGetList
    {
        /// <summary>
        /// 菜单类型，0 用户菜单，1：系统菜单，2：全部
        /// </summary>
        public int MenuType { set; get; }
    }

    public class Menu_UpdateOrderArg
    {
        public string MenuId { set; get; }

        public int OrderNo { set; get; }
    }

    public class MenuBLL : BLLCrudBase<Menu_ListArg, Sys_Menu>
    {
        protected override bool CheckSaveArg(Sys_Menu model, bool isAdd, out string msg)
        {
            if (!isAdd)
            {
                model.ClearChangedState(Sys_Menu.CNIsSystem, Sys_Menu.CNMenuLevel);
                model.SetFieldChanged(Sys_Menu.CNIsExpand);
            }
            msg = null;
            return true;
        }

        protected override ExecResult GetDelResult(string id)
        {
            throw new System.NotImplementedException();
        }

        protected override void SetQueryOption(Menu_ListArg arg, QueryCondition<Sys_Menu> query)
        {
            if (arg.MenuType == 0 || arg.MenuType == 1)
            {
                var isSysMenu = arg.MenuType == 1;
                query.WhereAnd(s => s.IsSystem == isSysMenu);
            }
            query.OrderBy(s => s.MenuLevel).OrderBy(s => s.OrderNo);
        }

        public override ApiResult Add(Sys_Menu model)
        {
            P_MenuAdd p = new P_MenuAdd();
            p.Arg_IsApi = model.IsApi ? 1 : 0;
            p.Arg_IsExpand = model.IsExpand ? 1 : 0;
            p.Arg_MenuIcon = model.MenuIcon;
            p.Arg_MenuName = model.MenuName;
            p.Arg_MenuUrl = model.MenuUrl;
            p.Arg_PId = model.PId;
            if (!p.Arg_MenuName.IsLenRange(3, null))
            {
                return this.RetErr("菜单名称格式不正确");
            }
            DB.ExecProcedure(p).ExecProc();
            if (p.OutCode == 0)
            {
                return this.RetOK();
            }
            return this.RetErr(p.OutMsg);
        }

        public override ApiResult Del(string menuId)
        {
            if (string.IsNullOrWhiteSpace(menuId))
            {
                return this.RetErr("菜单Id不能为空");
            }

            P_MenuDel p = new P_MenuDel();
            p.Arg_MenuId = menuId;
            DB.ExecProcedure(p).ExecProc();

            if (p.OutCode == 0)
            {
                return this.RetOK();
            }
            return this.RetErr(p.OutMsg);
        }

        public ApiResult UpdateOrder(Menu_UpdateOrderArg arg)
        {
            Sys_Menu t = new Sys_Menu();
            t.Id = arg.MenuId;
            t.OrderNo = arg.OrderNo;
            var r = DB.Update(t);
            if (r.RowCount == 1)
            {
                return this.RetOK("修改完成");
            }
            else
            {
                return this.RetErr("更新失败");
            }
        }
    }
}